package lijiale.project.employee.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lijiale.project.employee.model.EmployeeModel;
import lijiale.project.util.JDBCUtil;

public class EmployeeDao {

	public static Integer insert(EmployeeModel model) {
		String sql = "insert into employee(code,name,pass,code_market,tel) values (?,?,?,?,?) ";
		List<Object> vals = Arrays.asList(model.getCode(), model.getName(), model.getPass(), model.getCodeMarket(),
				model.getTel());
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql, vals);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	public static Integer deleteByCode(EmployeeModel model) {
		String sql = "delete from employee where code=?";
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql, model.getCode());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	public static Integer deleteByOther(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("delete from employee where 1=1");
		List<Object> vals = appendWhere(sql, model);
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql.toString(), vals);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	public static Integer updateAll(EmployeeModel model) {
		String sql = "update employee set name=? , pass=? ,code_market=? ,tel=? where code=? ";
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql, model.getName(), model.getPass(), model.getCodeMarket(), model.getTel(),
					model.getCode());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	public static Integer updateActive(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("update employee set id=id ");
		List<Object> vals = new ArrayList<>();
		String name = model.getName();
		if (name != null) {
			sql.append(",name=? ");
			vals.add(name);
		}
		String pass = model.getPass();
		if (pass != null) {
			sql.append(",pass=? ");
			vals.add(pass);
		}
		String codeMarket = model.getCodeMarket();
		if (codeMarket != null) {
			sql.append(",code_market=? ");
			vals.add(codeMarket);
		}
		String tel = model.getTel();
		if (tel != null) {
			sql.append(",tel =? ");
			vals.add(tel);
		}
		String image = model.getImage();
		if (image != null) {
			sql.append(",image =? ");
			vals.add(image);
		}
		sql.append(" where code=? ");
		vals.add(model.getCode());
		Integer i = 0;
		try {
			i = JDBCUtil.update(sql.toString(), vals);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	public static List<EmployeeModel> select(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("select *from employee where 1=1 ");
		List<Object> vals = appendWhere(sql, model);
		Map<String, String> fields = new HashMap<>();
		fields.put("code", "code");
		fields.put("name", "name");
		fields.put("pass", "pass");
		fields.put("codeMarket", "code_market");
		fields.put("tel", "tel");
		List<EmployeeModel> list = new ArrayList<>();
		try {
			list = JDBCUtil.queryList(sql.toString(), vals, EmployeeModel.class, fields);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public static EmployeeModel selectModel(EmployeeModel model) {
		if (select(model).size() == 0) {// 判断如果没找到数据，则返回null
			return null;
		} else
			return select(model).get(0);
	}

	public static List<Object> appendWhere(StringBuffer sql, EmployeeModel model) {
		List<Object> vals = new ArrayList<>();
		String code = model.getCode();
		if (code != null && code.trim().isEmpty()) {
			sql.append(" and code like ? ");
			vals.add(code);
		}
		String name = model.getName();
		if (name != null && name.trim().isEmpty()) {
			sql.append(" and name like ? ");
			vals.add(name);
		}
		String pass = model.getPass();
		if (pass != null && pass.trim().isEmpty()) {
			sql.append(" and pass like ? ");
			vals.add(pass);
		}
		String codeMarket = model.getCodeMarket();
		if (codeMarket != null && codeMarket.trim().isEmpty()) {
			sql.append(" and code_market like ? ");
			vals.add(codeMarket);
		}
		String tel = model.getTel();
		if (tel != null && tel.trim().isEmpty()) {
			sql.append(" and tel like ? ");
			vals.add(tel);
		}
		return vals;
	}

}
